package com.xx.controller;

import com.github.pagehelper.PageInfo;
import com.xx.entity.EmployeesRegister;
import com.xx.enums.HttpEnum;
import com.xx.entity.EmployeesLogin;
import com.xx.entity.Employees;
import com.xx.entity.R;
import com.xx.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("employees")
@CrossOrigin
public class EmployeesController{

    @Autowired
    EmployeesService employeesService;

    @GetMapping
    public R<PageInfo<Employees>> selectAll(Employees employees, Integer pageNum) {
        PageInfo<Employees> employeesPageInfo = employeesService.selectAllByPage(employees, pageNum);
        return R.success(employeesPageInfo);
    }


    @GetMapping("{id}")
    public R selectOne(@PathVariable Integer id) {
        Employees employees = employeesService.selectByEmployeeId(id);
        return R.success(employees);
    }


    @PostMapping
    public R insert(@RequestBody Employees employees) {
        if (employeesService.insertSelective(employees)>0){
            return R.success("新增成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"新增失败");
        }
    }


    @PutMapping
    public R update(@RequestBody Employees employees) {
        if (employeesService.updateByEmployeeId(employees,employees.getEmployeeId())>0){
            return R.success("修改成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"修改失败");
        }
    }


    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") Integer employeeId) {
        if (employeesService.deleteByEmployeeId(employeeId)>0){
            return R.success("删除成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"删除失败");
        }
    }


    @PostMapping("login")
    public R login(@RequestBody EmployeesLogin employeesLogin) {
        Employees employees = employeesService.selectByLoginName(employeesLogin.getUsername(),employeesLogin.getPassword());
        if (employees != null){
            return R.success(employees);
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"登录失败");
        }
    }


    @PostMapping("register")
    public R register(@RequestBody EmployeesRegister employeesRegister) {
        if (employeesService.registerEmployees(employeesRegister)>0){
            return R.success("注册成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"注册失败");
        }
    }

}


